﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.TextManager.Interop;

namespace BlunderBrain.VisualStudio.AviSynth
{
    public class AviSynthDeclarations : Declarations
    {
        private List<AviSynthFunction> m_declarations;
        private StringBuilder m_description;

        public AviSynthDeclarations()
        {
            m_declarations = new List<AviSynthFunction>();
            m_description = new StringBuilder();
        }

        public void AddDeclaration(AviSynthFunction declaration)
        {
            m_declarations.Add(declaration);
        }

        public void AddDeclarations(IEnumerable<AviSynthFunction> declarations)
        {
            m_declarations.AddRange(declarations);
        }

        public override int GetCount()
        {
            return m_declarations.Count;
        }

        public override string GetDescription(int index)
        {
            m_description.Clear();
            m_description.AppendFormat("{0}(", m_declarations[index].Name);
            for (int i = 0; i < m_declarations[index].Parameters.Length; i++)
            {
                AviSynthFunctionParameter param = m_declarations[index].Parameters[i];
                m_description.AppendFormat("{0} {1}{2}{1}{3}",
                    param.DataType.ToString().ToLower(),
                    param.Optional ? "\"" : "",
                    param.Name,
                    param.Repeating ? "[...]" : "");
                
                /*
                m_description.AppendFormat("{0}{1}{2}{3}{4}{5}{6}",
                    param.Optional ? "[" : "",
                    param.DataType.ToString().ToLower(),
                    param.Optional ? "]" : "",
                    string.IsNullOrEmpty(param.Name) ? "" : " <",
                    param.Name,
                    string.IsNullOrEmpty(param.Name) ? "" : ">",
                    param.Repeating ? "..." : "");
                */

                if (i + 1 < m_declarations[index].Parameters.Length)
                    m_description.Append(", ");
            }
            m_description.AppendFormat(")");

            return m_description.ToString();
        }

        public override string GetDisplayText(int index)
        {
            return GetName(index);
        }

        public override int GetGlyph(int index)
        {
            return (int)AviSynthLanguageService.IconImageIndex.Method;
        }

        public override string GetName(int index)
        {
            return index >= 0 && index < m_declarations.Count ? m_declarations[index].Name : null;
        }

        
        public override bool IsPerfectMatch(string textSoFar, int index)
        {
            // Make perfect matches case insensitive since AviSynth doesn't care about case
            return string.Compare(textSoFar, 0, m_declarations[index].Name, 0, textSoFar.Length, true) == 0;
        }
    }
}
